﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRSC_IN010201UV01_Jcyy;
using YLW.Plat.Jhfw.Model.JhfwJcyy;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRSC_IN010201UV01_Jcyy
    {
        public static string CheckAppointStatusInfoUpdate(AddJhfwJcyyIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRSC_IN010201UV01
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRSC_IN010201UV01" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[] {
                               new item {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = receiver_name
                                }
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[] {
                                new item {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = sender_name
                                }
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        actAppointment = new actAppointment
                        {
                            classCode = "ACT",
                            moodCode = "APT",
                            id = new id { extension = jsonData.jzlsh },
                            code = new code
                            {
                                code1 = "R",
                                codeSystem = "2.16.156.10011.2.3.1.271",
                                displayName = new displayName { value = "门诊" }
                            },
                            effectiveTime = new effectiveTime
                            {
                                any = new any
                                {
                                    value = jsonData.yyjcsj
                                }
                            },
                            subject = new subject
                            {
                                typeCode = "SUBJ",
                                patient = new patient
                                {
                                    classCode = "PAT",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.5",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.4",
                                                extension=jsonData.mrn
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.1.10",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.1.12",
                                                extension=""
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.8",
                                                extension=jsonData.series
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.9",
                                                extension=jsonData.jzlsh
                                            },
                                            new item
                                            {
                                                root="2.16.156.10011.2.5.1.11",
                                                extension=jsonData.yyxh
                                            }
                                        }
                                    },
                                    patientPerson = new patientPerson
                                    {
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part { value = jsonData.pname }
                                            }
                                        }
                                    },
                                    providerOrganization = new providerOrganization
                                    {
                                        classCode = "ORG",
                                        determinerCode = "INSTANCE",
                                        id = new id
                                        {
                                            item = new item[]
                                            {
                                                new item{ root="2.16.156.10011.1.26", extension=jsonData.ksdm }
                                            }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part { value = jsonData.ksmc }
                                            }
                                        },
                                        contactParty = new contactParty
                                        {
                                            classCode = "CON"
                                        }
                                    }
                                }
                            },
                            reusableDevice = new reusableDevice
                            {
                                typeCode = "RDV",
                                manufacturedDevice = new manufacturedDevice
                                {
                                    classCode = "MANU",
                                    manufacturedDevice1 = new manufacturedDevice
                                    {
                                        classCode = "DEV",
                                        determinerCode = "INSTANCE",
                                        id = new id { extension = jsonData.sbdm },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part
                                                {
                                                    value = jsonData.sbmc
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            performer = new performer
                            {
                                typeCode = "PRF",
                                assignedPerson = new assignedPerson
                                {
                                    classCode = "ASSIGNED",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item{ root="2.16.156.10011.1.4", extension=jsonData.yyrgh }
                                        }
                                    },
                                    assignedPerson1 = new assignedPerson
                                    {
                                        classCode = "PSN",
                                        determinerCode = "INSTANCE",
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part { value = jsonData.yyrxm }
                                            }
                                        }
                                    }
                                }
                            },
                            location = new location
                            {
                                typeCode = "LOC",
                                serviceDeliveryLocation = new serviceDeliveryLocation
                                {
                                    classCode = "SDLOC",
                                    id = new id
                                    {
                                        item = new item[]
                                        {
                                            new item{ root="2.16.156.10011.1.26", extension=jsonData.zxksdm }
                                        }
                                    },
                                    addr = new addr
                                    {
                                        item = new item
                                        {
                                            part = new part
                                            {
                                                value = jsonData.zxksmc
                                            }
                                        }
                                    }
                                }
                            },
                            referencedOrder = new referencedOrder[]
                            {
                                new referencedOrder
                                {
                                    typeCode="OREF",
                                    actOrder=new actOrder
                                    {
                                        classCode="ACT",
                                        moodCode="RQO",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item{ root="2.16.156.10011.1.24", extension=jsonData.sqdh }
                                            }
                                        }
                                    }
                                },
                                new referencedOrder
                                {
                                    typeCode="OREF",
                                    actOrder=new actOrder
                                    {
                                        classCode="ACT",
                                        moodCode="RQO",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item{ root="2.16.156.10011.1.28", extension=jsonData.yzid }
                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                dotype = new value { value1 = jsonData.dotype },
                                sjly = new value { value1 = jsonData.sjly },
                                lyxt = new value { value1 = jsonData.lyxt },
                                hoscode = new value { value1 = jsonData.hoscode },
                                yyid = new value { value1 = jsonData.yyid },
                                jzlsh = new value { value1 = jsonData.jzlsh },
                                sqdh = new value { value1 = jsonData.sqdh },
                                //tmh = new value { value1 = jsonData.tmh },
                                //bbdm = new value { value1 = jsonData.bbdm },
                                yzid = new value { value1 = jsonData.yzid },
                                mrn = new value { value1 = jsonData.mrn },
                                pname = new value { value1 = jsonData.pname },
                                series = new value { value1 = jsonData.series },
                                stay = new value { value1 = jsonData.stay },
                                ksdm = new value { value1 = jsonData.ksdm },
                                ksmc = new value { value1 = jsonData.ksmc },
                                yyjcsj = new value { value1 = jsonData.yyjcsj },
                                yyxh = new value { value1 = jsonData.yyxh },
                                zxksdm = new value { value1 = jsonData.zxksdm },
                                zxksmc = new value { value1 = jsonData.zxksmc },
                                sbdm = new value { value1 = jsonData.sbdm },
                                sbmc = new value { value1 = jsonData.sbmc },
                                yyrgh = new value { value1 = jsonData.yyrgh },
                                yyrxm = new value { value1 = jsonData.yyrxm },
                                //jcdd = new value { value1 = jsonData.jcdd },
                                //zysx = new value { value1 = jsonData.zysx },
                                //remark = new value { value1 = jsonData.remark },
                                useflag = new value { value1 = jsonData.useflag },
                                in_empid = new value { value1 = jsonData.in_empid },
                                in_date = new value { value1 = jsonData.in_date },
                                xg_empid = new value { value1 = jsonData.xg_empid },
                                xgsj = new value { value1 = jsonData.xgsj }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
